package com.jike.noobmoney.mvp.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.adapter.MyPushListAdapter;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.MyPushEntity;
import com.jike.noobmoney.mvp.presenter.TaskPresenter;
import com.jike.noobmoney.mvp.view.activity.PushTaskUpdateActivity;
import com.jike.noobmoney.mvp.view.activity.ShenHeActivity;
import com.jike.noobmoney.mvp.view.activity.UserTaskDetailActivity;
import com.jike.noobmoney.mvp.view.widget.JiaPiaoDialog;
import com.jike.noobmoney.mvp.view.widget.TopTaskDialog;
import com.jike.noobmoney.net.IView;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 我的发布
 */
public class MyPushFragment extends BaseNewFragment implements IView {

    public static String Channels = "mypush";
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.empty)
    ImageView empty;


    private String status;
    private String userid;
    private MyPushListAdapter adapter;
    private List<MyPushEntity.TaskBean> mData = new ArrayList<>();
    private TaskPresenter taskPresenter;
    private int page = 1;
    private boolean isRefresh = true;


    @Override
    protected int layoutResID() {
        return R.layout.fragment_my_push;
    }

    @Override
    protected void lazyLoad() {
        if (!isPrepared || !isVisible || mHasLoadedOnce) {
            return;
        }
        mHasLoadedOnce = true;
        taskPresenter = new TaskPresenter(this);
        initData();
    }

    protected void initData() {
        Bundle bundle = getArguments();
        if (bundle != null) {
            status = bundle.getString(MyPushFragment.Channels);
        }
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);

        getData(true);

        //adapter
        adapter = new MyPushListAdapter(context);
        //manager
        recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(refreshlayout -> {
            getData(true);
        });

        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            getData(false);
        });

        adapter.setOnItemClickListener((adapterView, view, position, l) -> {
//            MyPushEntity.TaskBean item = (MyPushEntity.TaskBean) adapter.getItem(position);
//            int t_id = item.getT_id();
//            Intent intent = new Intent(context, UserTaskDetailActivity.class);
//            intent.putExtra(UserTaskDetailActivity.t_id, "" + t_id);
//            startActivity(intent);
        });

        adapter.setChildClickLis(new MyPushListAdapter.ChildClickLis() {
            @Override
            public void shenhe(String t_id) {
                //审核
                ShenHeActivity.startAct(context, t_id);
            }

            @Override
            public void tuikaun(String t_id) {
                //退款
                tuikuan(t_id);
            }

            @Override
            public void jiapiao(String t_id) {
                //加票
                JiaPiaoDialog jiaPiaoDialog = JiaPiaoDialog.getInstance();
                jiaPiaoDialog.show(getFragmentManager(), "jiapiao");
                jiaPiaoDialog.setClickCallback(number -> {
                    //确认
                    jiaPiaoDialog.dismiss();
                    jiapiaoReq(t_id, number);
                });
            }

            @Override
            public void update(String t_id) {
                Intent intent= new Intent(context,PushTaskUpdateActivity.class);
                intent.putExtra(UserTaskDetailActivity.t_id,t_id);
                context.startActivity(intent);
            }

            @Override
            public void top(MyPushEntity.TaskBean item) {
                topDialog(item);
            }

            @Override
            public void up(String t_id) {
                showProgressDialog();
                taskPresenter.up(t_id, ConstantValue.RequestKey.up);
            }

            @Override
            public void down(String t_id) {
                showProgressDialog();
                taskPresenter.down(t_id, ConstantValue.RequestKey.down);
            }
        });

    }

    /**
     * 退款
     */
    private void tuikuan(String t_id) {
        showProgressDialog();
        taskPresenter.tuikuan(t_id, ConstantValue.RequestKey.my_push_tuikuan);
    }

    /**
     * 加票
     */
    private void jiapiaoReq(String t_id, String number) {
        showProgressDialog();
        taskPresenter.jiapiao(t_id, number, ConstantValue.RequestKey.my_push_jiapiao);
    }
    private void topDialog(MyPushEntity.TaskBean item)
    {
        TopTaskDialog topTaskDialog = TopTaskDialog.newInstance(item.getT_id()+"",item.topmoney);
        topTaskDialog.show(getFragmentManager(), "loginout");
        topTaskDialog.setClickCallback(new TopTaskDialog.OnClickCallback() {
            @Override
            public void confirm(String t_id, String money) {
                topTaskDialog.dismiss();
                top(t_id,money);
            }
        });
    }
    public void top(String tid,String num){
        taskPresenter.top(tid,num,ConstantValue.RequestKey.top);
    }

    /**
     * 获取数据
     */
    private void getData(boolean isRefresh) {
        this.isRefresh = isRefresh;
        if (isRefresh) {
            page = 1;
        } else {
            page++;
        }
        showProgressDialog();
        taskPresenter.getMyPush(userid, status, "" + page, "10", ConstantValue.RequestKey.my_push);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 200) {
            getData(true);
        }
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        disLoading();
        if (ConstantValue.RequestKey.my_push.equals(actionType)) {
            List<MyPushEntity.TaskBean> orderlist = (List<MyPushEntity.TaskBean>) data;
            if (orderlist != null && orderlist.size() > 0) {
                if (isRefresh) {
                    mData.clear();
                    mData.addAll(orderlist);
                } else {
                    mData.addAll(orderlist);
                }
            }

            if (empty != null) {
                if (mData.size() > 0) {
                    empty.setVisibility(View.GONE);
                } else {
                    empty.setVisibility(View.VISIBLE);
                }
            }
//            if (isRefresh) {
//                if (orderlist != null && orderlist.size() > 0) {
//                    empty.setVisibility(View.GONE);
//                    mData.clear();
//                    mData.addAll(orderlist);
//                } else {
//                    mData.clear();
//                    empty.setVisibility(View.VISIBLE);
//                }
//            } else {
//                if (orderlist != null) {
//                    empty.setVisibility(View.GONE);
//                    mData.addAll(orderlist);
//                }
//            }
            adapter.refresh(mData);
        }


        if (ConstantValue.RequestKey.my_push_tuikuan.equals(actionType)) {
            ToastUtils.showShortToastSafe((String) data);
            if (getActivity() != null) {
                getActivity().finish();
            }
        }
        if (ConstantValue.RequestKey.up.equals(actionType)) {
            ToastUtils.showShortToastSafe((String) data);
            if (getActivity() != null) {
                getActivity().finish();
            }
        }
        if (ConstantValue.RequestKey.down.equals(actionType)) {
            ToastUtils.showShortToastSafe((String) data);
            if (getActivity() != null) {
                getActivity().finish();
            }
        }

        if (ConstantValue.RequestKey.my_push_jiapiao.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            if (getActivity() != null) {
                getActivity().finish();
            }
        }
        if (ConstantValue.RequestKey.top.equals(actionType)) {
            ToastUtils.showShortToastSafe(msg);
            if (getActivity() != null) {
                getActivity().finish();
            }
        }


    }

    @Override
    public void onFailed(String code, String msg) {
        disLoading();
        ToastUtils.showShortToastSafe(msg);
    }

    private void disLoading() {
        dismissProgressDialog();
        if (refreshLayout != null) {
            refreshLayout.finishRefresh();
            refreshLayout.finishLoadMore();
        }


    }
}
